//
//  GoodsViewController.m
//  IGOtest
//
//  Created by administrator on 14-2-20.
//  Copyright (c) 2014年 administrator. All rights reserved.
//

#import "GoodsViewController.h"
#import "Goods.h"
#import "CustomCell.h"
#import "igoods.h"
#import "GoodsdetViewController.h"
#import "AppDelegate.h"
#define  GOODSAPI @"http://10.207.1.23:8888/user/goods.php"

@interface GoodsViewController ()<NSURLConnectionDataDelegate>
{
    NSMutableArray *_mutableArr;
    AppDelegate *del;
}


@end

@implementation GoodsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    del = [[UIApplication sharedApplication]delegate];
    
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 80, 50) ];
    [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btn setTitle:@"< Back" forState:UIControlStateNormal];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(130, 0, 80, 50)];
    label.textColor = [UIColor greenColor];
    label.text = @"商品列表";
    [self.view addSubview:label];
    _main.dataSource = self;
    _main.delegate =self;
//    //定义URL
//    NSURL *url = [NSURL URLWithString:GOODSAPI];
//    //定义请求
//    NSURLRequest *goodsRequest = [[NSURLRequest alloc]initWithURL:url];
//    NSError *error = nil;
//    //发送商品请求（同步）,返回NSData类型的请求数据
//    NSData *data = [NSURLConnection sendSynchronousRequest:goodsRequest returningResponse:nil error:&error];
//    //JSon解析，解析结果为字典
//    NSArray *goodsArr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
//    NSLog(@"%@",goodsArr);
//    del.dataList = [NSMutableArray array];
//    if (goodsArr != nil) {
//        for (int i=0;i<[goodsArr count];i++) {
//            NSDictionary *goods = goodsArr[i];
//            NSString *name = [goods objectForKey:@"goodsname"];
//            NSString *price = [goods objectForKey:@"price"];
//            NSString *comment = [goods objectForKey:@"comment"];
//            NSString *image = [goods objectForKey:@"image"];
//            NSURL *url = [[NSURL alloc]initWithString:image];
//            NSData *imageData = [[NSData alloc]initWithContentsOfURL:url];
//            //UIImage *img2 = [[UIImage alloc] initWithContentsOfFile:image];
//            UIImage *img = [UIImage imageWithData:imageData];
//            igoods *thegoods = [[igoods alloc]initWithGoodsname:name igoodsprice:price igoodscomment:comment igoodsimage:img];
//            [del.dataList addObject:thegoods];
//            NSLog(@"====%@",del.dataList);
//
//        }
//    }
    [_main reloadData];
}
-(void)btnclick:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    AppDelegate *del = [[UIApplication sharedApplication]delegate ];
    
    return [del.dataList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CustomCellIdentifier = @"CustomCellIdentifier";
    
    BOOL nibsRegistered = NO;
    if (!nibsRegistered) {
        UINib *nib = [UINib nibWithNibName:@"CustomCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:CustomCellIdentifier];
        nibsRegistered = YES;
    }
    
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    if (cell == nil) {
        cell = [[CustomCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:CustomCellIdentifier];
    }
    NSUInteger row = [indexPath row];
    cell.name = [[del.dataList objectAtIndex:row] igoodsname];
    cell.dec = [[del.dataList objectAtIndex:row] igoodscomment];
    cell.loc = [[del.dataList objectAtIndex:row] igoodsprice];
    cell.image = [[del.dataList objectAtIndex:row] igoodsimage];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
    
}
#pragma mark Table Delegate Methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60.0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GoodsdetViewController *goodsdet = [[GoodsdetViewController alloc]initWithNibName:@"GoodsdetViewController" bundle:nil ];
    goodsdet.goods = del.dataList[indexPath.row];
    [self presentViewController:goodsdet animated:YES completion:nil];
}
@end
